var mecsArray2 = new Array("KXF-I1","KXF-I2","KXF-II1","KXF-II2","HPF-I1","HPF-I2","HPF-II1","HPF-II2");
var list2 = new Array();

emcs2_datadeal_success=function (result) {
    list2 = [];
    var data = result.data;

    if(data.length>0){
        var index = 0;
        for(var i=0;i<data.length;i++){
            if(i == 0 || i % 18 == 0){
                var object = new Object();
                object.ID = mecsArray2[index];
                object.S02 = data[i];
                object.S01 = data[i+1];
                object.S20 = data[i+2];
                object.S17 = data[i+3];
                object.A01 = data[i+4];
                object.S19 = data[i+5];
                object.A03 = data[i+6];
                object.N01 = data[i+7];
                object.T02 = data[i+8];
                object.T03 = data[i+9];
                object.T04 = data[i+10];
                object.T05 = data[i+11];
                object.D02 = data[i+12];
                object.C02 = data[i+13];
                object.C01 = data[i+14];
                object.C07 = data[i+15];
                object.C08 = data[i+16];
                object.A02 = data[i+17];

                list2.push(object);
                index++;
            }
        }

    }

    if(list2.length>0){
        for(var i=0;i<list2.length;i++){
            var id = list2[i].ID;
            var s02 = list2[i].S02;
            var s01 = list2[i].S01;
            var s20 = list2[i].S20;
            var s17 = list2[i].S17;
            var a01 = list2[i].A01;
            var a02 = list2[i].A02;
            var a03 = list2[i].A03;
            var s19 = list2[i].S19;
            var n01 = list2[i].N01;
            var t02 = list2[i].T02;
            var t03 = list2[i].T03;
            var t04 = list2[i].T04;
            var t05 = list2[i].T05;
            var d02 = list2[i].D02;
            var c02 = list2[i].C02;
            var c01 = list2[i].C01;
            var c07 = list2[i].C07;
            var c08 = list2[i].C08;

            if(s02 == '1' && s01 == '0'
                && (parseInt(a01) + parseInt(a02) + parseInt(a03) == 0)
                && s17 == '1'){
                //绿变
                $("#"+id).attr("src","../../../static/images/fengji/xiao/fengji_green.gif");
                $("#TxtEmcs"+id).html('变');
                $("#TxtDiv"+id).css("color","#4EA01B");
            }else if(s02 == '1' && s01 == '0'
                && (parseInt(a01) + parseInt(a02) + parseInt(a03) == 0)
                && s17 == '0'){
                //绿工
                $("#"+id).attr("src","../../../static/images/fengji/xiao/fengji_green.gif");
                $("#TxtEmcs"+id).html('工');
                $("#TxtDiv"+id).css("color","#4EA01B");
            }else if(s02 == '0' && s01 == '1'
                && (parseInt(a01) + parseInt(a02) + parseInt(a03) == 0)
            ){
                //灰色
                $("#"+id).attr("src","../../../static/images/fengji/xiao/fengji_grey.png");
                $("#TxtEmcs"+id).html('');
                //$("#TxtDiv"+id).attr("class","");
            }else if(s02 == '1' && s01 == '0'
                && (parseInt(a01) + parseInt(a02) + parseInt(a03) != 0)
                && s17 == '1'){
                //黄变
                $("#"+id).attr("src","../../../static/images/fengji/xiao/fengji_yellow_run.gif");
                $("#TxtEmcs"+id).html('变');
                $("#TxtDiv"+id).css("color","#DFBC00");
            }else if(s02 == '1' && s01 == '0'
                && (parseInt(a01) + parseInt(a02) + parseInt(a03) != 0)
                && s17 == '0'){
                //黄工
                $("#"+id).attr("src","../../../static/images/fengji/xiao/fengji_yellow_run.gif");
                $("#TxtEmcs"+id).html('工');
                $("#TxtDiv"+id).css("color","#DFBC00");
            }else if(s02 == '0' && s01 == '1'
                && (parseInt(a01) + parseInt(a02) + parseInt(a03) != 0)){
                //黄停
                $("#"+id).attr("src","../../../static/images/fengji/xiao/fengji_yellow_stop.png");
                $("#TxtEmcs"+id).html('');
                //$("#TxtDiv"+id).attr("class","");
            }else if((parseInt(s01) + parseInt(s02) != 1)){
                //蓝色
                $("#"+id).attr("src","../../../static/images/fengji/xiao/fengji_blue.png");
                $("#TxtEmcs"+id).html('');
                //$("#TxtDiv"+id).attr("class","");
            }

            if($("#dxthpfj").css('display')=='block'){
                if(devcode==id){
                    fuzhi2(devcode);
                }
            }
        }
    }
}

emcs2_datadeal_error=function () {
    //未知状态
    if(mecsArray2.length > 0){
        for(var i=0;i<mecsArray2.length;i++){
            $("#"+mecsArray2[i]).attr("src","../../../static/images/fengji/xiao/fengji_blue.png");
            $("#TxtEmcs"+mecsArray2[i]).html('');
            //$("#TxtDiv"+mecsArray2[i]).attr("class","");
        }
    }
};

//获取风机数据
function getMecsStatus2(){
    if(comm_rundemo){
        var _tmpdata={
            "code": "200",
            "type": null,
            "message": null,
            "data": [
                "1",
                "0",
                "1",
                "1",
                "0",
                "0",
                "0",
                "1",
                "45",
                "0",
                "0",
                "0",
                "40",
                "0",
                "0",
                "1",
                "0",
                "0",
                "1",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "1",
                "0",
                "0",
                "1",
                "0",
                "0",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "1",
                "0",
                "0",
                "0",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "1",
                "1",
                "0",
                "0",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "1",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0",
                "0"
            ]
        };
        emcs2_datadeal_success(_tmpdata);
    }
    else {
        $.ajax({
         type:'post',
         url: targetPath + '/rest/getData?start=v90&end=v233',
         dataType: 'json',//here
         success: function(result)
         {
         if(result.code=="200"){
         emcs2_datadeal_success(result);
         }
         else{
         emcs2_datadeal_error();
         }
         }
         });
    }


}

function show2(obj){
	if($("#zhsktq").css('display')=='block'){
		$("#zhsktq").hide();
	}
	$("#dxthpfj").show();	
	devcode = obj;
	fuzhi2(devcode);
}

function hide2(){
	$("#dxthpfj").hide();
	$("#plsd2").val("");
}

function fuzhi2(devcode){
	var a=0;
	if(null != devcode && '' != devcode){
		for(var i=0;i<list2.length;i++){
			if(list2[i].ID == devcode){
				a = i;
				break;
			}else if(i==(list2.length-1) && list2[i].ID != devcode){
				return;
			}
		}
		
		$("#biaotiID2").html(devcode);
		$("#devID2").html(devcode);
		
		if(list2[a].S02=='1' && list2[a].S01=='0'){//运行状态 0无，1运行
		
			$("#ydzt2").html("运行");
		
		}else if(list2[a].S02=='0' && list2[a].S01=='1'){
			
			$("#ydzt2").html("停止");
		
		}else if((list2[a].S02 == 1 && list2[a].S01 == 1) 
				|| (list2[a].S02==0 && list2[a].S01==0)){
			
			$("#ydzt2").html("异常");
			
		}
		
		if(list2[a].A01 == '0'){
			$("#gzgz2").html("无");
		}else if(list2[a].A01 == '1'){
			$("#gzgz2").html("故障");
		}
		
		if(list2[a].A02 == '0'){
			$("#bpqtxgz2").html("无");
		}else if(list2[a].A02 == '1'){
			$("#bpqtxgz2").html("通信故障");
		}
		
		if(list2[a].A03 == '0'){
			$("#bpqgzbj2").html("无");
		}else if(list2[a].A03 == '1'){
			$("#bpqgzbj2").html("故障");
		}
		
		if(list2[a].S19 == '0'){
			$("#hzms2").html("无");
		}else if(list2[a].S19 == '1'){
			$("#hzms2").html("火灾运行");
		}
		
		if(list2[a].N01 == '0'){
			$("#ls2").html("不满足");
		}else if(list2[a].N01 == '1'){
			$("#ls2").html("满足");
		}
		
		if(list2[a].S20 == '0'){
			$("#dqczcs2").html("就地");
		}else if(list2[a].S20 == '1'){
			$("#dqczcs2").html("远方");
		}
		
		$("#ydpl2").html(list2[a].T02+"HZ");
		
		$("#ljyxsj2").html(list2[a].T03+"小时");
		
		$("#sxpjdl2").html(list2[a].T04+"A");
		
		$("#ygdd2").html(list2[a].T05+"KWh");
		
		
	}
}

//运行控制
function yxkz2(){
	var a = getXiabiao2();
	var S20 = list2[a].S20; 
	var N01 = list2[a].N01;
	if(S20 == '1' && N01 == '1'){
		//输出C02=1
		var num;
		var val = "1";

		if(list2[a].ID == "KXF-I1"){
			num = "103";
		}else if(list2[a].ID == "KXF-I2"){
			num = "121";
		}else if(list2[a].ID == "KXF-II1"){
			num = "139";
		}else if(list2[a].ID == "KXF-II2"){
			num = "157";
		}else if(list2[a].ID == "HPF-I1"){
			num = "175";
		}else if(list2[a].ID == "HPF-I2"){
			num = "193";
		}else if(list2[a].ID == "HPF-II1"){
			num = "211";
		}else if(list2[a].ID == "HPF-II2"){
			num = "229";
		}else{
			alert("无该设备！");
			return;
		}
		//修改值
		writeData(num,val);
		
	}else if(S20 == '0'){
		alert("设备处于就地控制状态！");
	}else if(N01 == '0'){
		alert("设备联锁条件不满足！");
	}
	
}

//停止控制
function tzkz2(){
	var a = getXiabiao2();
	var S20 = list2[a].S20; 
	
	if(S20=='1'){
		var num;
		var val = "1";

		if(list2[a].ID == "KXF-I1"){
			num = "104";
		}else if(list2[a].ID == "KXF-I2"){
			num = "122";
		}else if(list2[a].ID == "KXF-II1"){
			num = "140";
		}else if(list2[a].ID == "KXF-II2"){
			num = "158";
		}else if(list2[a].ID == "HPF-I1"){
			num = "176";
		}else if(list2[a].ID == "HPF-I2"){
			num = "194";
		}else if(list2[a].ID == "HPF-II1"){
			num = "212";
		}else if(list2[a].ID == "HPF-II2"){
			num = "230";
		}else{
			alert("无该设备！");
			return;
		}
		writeData(num,val);
	}else if(S20=='0'){
		alert("设备处于就地控制状态！");
	}else{
		alert("数据异常");
	}
}


//得到对应下标
function getXiabiao2(){
	var a=0;
	var devcode = $("#devID2").html();
	for(var i=0;i<list2.length;i++){
		if(list2[i].ID == devcode){
			a = i;
			break;
		}else if(i==(list2.length-1) && list2[i].ID != devcode){
			return;
		}
	}
	return a;
}

//频率设定
function plsd2(){
	var a = getXiabiao2();
	var S20 = list2[a].S20;
	var D02 = $("#plsd2").val();

	if(D02 == "" || D02 == " " || D02 == undefined){
		alert("输入不能为空(频率设定范围为25~50Hz！)");
	}else if(S20 == '1' && ( parseInt(D02) >= 25 && parseInt(D02)<=50 )){
		//更改D02
		var num;

		if(list2[a].ID == "KXF-I1"){
			num = "98";
		}else if(list2[a].ID == "KXF-I2"){
			num = "116";
		}else if(list2[a].ID == "KXF-II1"){
			num = "134";
		}else if(list2[a].ID == "KXF-II2"){
			num = "152";
		}else if(list2[a].ID == "HPF-I1"){
			num = "170";
		}else if(list2[a].ID == "HPF-I2"){
			num = "188";
		}else if(list2[a].ID == "HPF-II1"){
			num = "206";
		}else if(list2[a].ID == "HPF-II2"){
			num = "224";
		}else{
			alert("无该设备！");
			return;
		}
		
		$("#ydpl2").html(D02+"HZ");
		writeData(num,D02);
		
	}else if(S20 == '0' && ( parseInt(D02) >=25 && parseInt(D02)<=50 )){
		alert("设备处于就地控制状态！");
	}else if(S20 == '1' && ( parseInt(D02) <25 || parseInt(D02)>50 )){
		alert("频率设定范围为25~50Hz！");
	}else{
		alert("频率设定范围为25~50Hz！");
	}
	
}

//火灾运行
function hzyx(){
	var a = getXiabiao2();
	var S20 = list2[a].S20; 
	var N01 = list2[a].N01; 
	var S19 = list2[a].S19;
	
	if(S20 == '1' && N01 == '1' && S19 == '0'){
		var num;
		var val = 1;
		
		if(list2[a].ID == "KXF-I1"){
			num = "105";
		}else if(list2[a].ID == "KXF-I2"){
			num = "123";
		}else if(list2[a].ID == "KXF-II1"){
			num = "141";
		}else if(list2[a].ID == "KXF-II2"){
			num = "159";
		}else if(list2[a].ID == "HPF-I1"){
			num = "177";
		}else if(list2[a].ID == "HPF-I2"){
			num = "195";
		}else if(list2[a].ID == "HPF-II1"){
			num = "213";
		}else if(list2[a].ID == "HPF-II2"){
			num = "231";
		}else{
			alert("无该设备！");
			return;
		}
		
		writeData(num,val);
	}else{
		alert("数据异常！");
	}
}

//灾后停止
function zhtz(){
	var a = getXiabiao2();
	var S20 = list2[a].S20;  
	var S19 = list2[a].S19;
	
	if(S20 == '1' && S19 == '0'){
		var num;
		var val = 1;
		
		if(list2[a].ID == "KXF-I1"){
			num = "109";
		}else if(list2[a].ID == "KXF-I2"){
			num = "124";
		}else if(list2[a].ID == "KXF-II1"){
			num = "142";
		}else if(list2[a].ID == "KXF-II2"){
			num = "160";
		}else if(list2[a].ID == "HPF-I1"){
			num = "178";
		}else if(list2[a].ID == "HPF-I2"){
			num = "196";
		}else if(list2[a].ID == "HPF-II1"){
			num = "214";
		}else if(list2[a].ID == "HPF-II2"){
			num = "232";
		}else{
			alert("无该设备！");
			return;
		}
		
		writeData(num,val);
	}else{
		alert("数据异常！");
	}
}

setInterval(function() {
	getMecsStatus2();
}, 3000);